// pages/comment/comment.js
const app = getApp()
// import api from '../../utils/request'
// import  AUTH from '../../utils/auth'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "cnt":0,// 总评论数
    "comment":{
      "id":null,
      "content":""
    },
    "moreLoading":false,
    // 当前评论item的路径
    "currentPath":null,
    // 当前评论的item
    "currentItem":null,
    "content":"",
    "showKeyboard":false,// 是否定位到输入框
    "currentPage":1,
    "more":true,
    "comList":[],
    "placeholder":"0",
    "reply": "",
    "like_id": ""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id=options.id;
    if(id==undefined){
      id=10
    }
    // 设置请求
    console.log("comment jwt", app.globalData.jwt)
    var that = this
    wx.request({
      url : "http://" + app.globalData.url + "/cook/comment/" + app.globalData.comment_id + "/",
      method: 'GET',
      header : { "jwt": app.globalData.jwt},
      data: {
        "code": options.id,
        "user_id": wx.getStorageSync('user_id')
      
      },
      success: res => {
        console.log("that, comment", res)
        this.setData({
          comList: res.data.commentlist,
        }
        )
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getComment()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.request({
      url : "http://" + app.globalData.url + "/cook/comment/" + app.globalData.comment_id + "/",
      method: 'GET',
      header : { "jwt": app.globalData.jwt},
      data: {
        "user_id": wx.getStorageSync('user_id')
      },
      success: res => {
        console.log("that, comment", res)
        this.setData({
          comList: res.data.commentlist,
        }
        )
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  comtentInput:function(e){
    this.setData({"comment.content": e.detail.value});
  },
  getComment:function(isMore){
    this.setData({ "moreLoading": true })
    api.getRequest('/api/frontend/wx/comment/',{"id":this.data.comment.id,"page":this.data.currentPage}).then(res=>{
      if(isMore){
        this.setData({ "comList": this.data.comList.concat(res.data.data.results), "cnt": res.data.data.count})
      }else{
        this.setData({ "comList": res.data.data.results, "cnt": res.data.data.count})
      }
      this.setData({ "moreLoading": false })
      if(res.data.data.next){
        this.setData({"more":true})
      }else{
        this.setData({"more":false})
      }

    })

  },
  onReachBottom() {
    // 加载更多
    if(this.data.more){
      this.setData({"currentPage":this.data.currentPage+1})
      this.getComment(true)
    }else{
      wx.showToast({
        title: '到底了',
        icon:"none"
      })
    }
  },
  // 点击评论
  inReply(e){
    var self = this;
     this.setData({ "isLoginPopup": true })
  
    var index = e.currentTarget.dataset.index;
    var cIndex = e.currentTarget.dataset.cindex
    let item;
    // 记录下item 的路径
    let path = ""
    if(index== undefined){
      path =''
      item=null;
      this.setData({ "placeholder": '请留下您的评论!' })
    }
    else if (cIndex == undefined) {
      path = `comList[${index}]`
      item = this.data.comList[index]
      this.setData({ "placeholder": `回复${item.username}:` , "reply": `${item.id}`})
    } else {
      path = `comList[${index}].chils[${cIndex}]`
      item = this.data.comList[index].chils[cIndex]
      this.setData({ "placeholder": `回复${item.username}:`  , "reply": `${item.id}`})
    }
    this.setData({"showKeyboard":true,"currentPath":path,"currentItem":item})
  },
  exitInput: function () {
    console.log("市区焦点")
    this.setData({"showKeyboard":false})
  },
  // 评论点赞
  commentLike(e) {
    console.log(e)

    var index = e.currentTarget.dataset.index;
    var cIndex = e.currentTarget.dataset.cindex;
    var item = this.data.comList[index]

    let items;
    items = this.data
    if (cIndex == undefined) {
      this.data.like_id = items.comList[index].id
      this.childs = false
    } else {
      this.data.like_id = item.child[cIndex].id
      this.childs = true
    }
    console.log("this childs", this.childs, items.comList[index].is_like)
    wx.request({
      url : "http://" + app.globalData.url + "/cook/comment/like/" + this.data.like_id + "/",
      method: 'POST',
      header: {"token": app.globalData.jwt},
      data: {
        "jwt": app.globalData.jwt,
        "user_id": wx.getStorageSync('user_id')
            },
      success: res => {
        // 取消点赞
        if(res.status == 0){
          // 子评论
            if(this.childs){
                items.comList[index].child[cIndex].is_like = false
                items.comList[index].child[cIndex].like-= 1
            }else{
                items.comList[index].is_like = false
                items.comList[index].like -= 1
            }
        }else{
          if(this.childs){
            items.comList[index].child[cIndex].is_like = true
            items.comList[index].child[cIndex].like+= 1
        }else{
            items.comList[index].is_like = true
            items.comList[index].like += 1
        }
        }
        console.log("this childs", this.childs, items.comList[index].is_like)

        if(res.data.status==1 || res.data.status ==0){
        wx.showToast({
            title: res.data.msg
          },

          )
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
        wx.request({
          url : "http://" + app.globalData.url + "/cook/comment/" + app.globalData.comment_id + "/",
          method: 'GET',
          header : { "jwt": app.globalData.jwt},
          data: {
            "user_id": wx.getStorageSync('user_id')
          },
          success: res => {
            console.log("that, comment", res)
            this.setData({
              comList: res.data.commentlist,
            }
            )
          }
        })
      }
    })

  },
  // 评论输入值监听
  commentValue(e){
    console.log(e)
    var content=e.detail.value
    this.setData({"content":content})
  },
  // 提交评论
  submitComment(options){
    var self = this;
    this.setData({ "isLoginPopup": true })
    let path = this.data.currentPath;
    let item = this.data.currentItem;
    var content=this.data.content;
    var reply=item?item.id:null
    // 设置请求
    console.log("comment jwt", app.globalData.jwt, self.data.content)
    wx.request({
      url : "http://" + app.globalData.url + "/cook/comment/" + app.globalData.comment_id + "/",
      method: 'POST',
      data: {
        "code": app.globalData.comment_id,
        "content": self.data.content,
        "jwt": app.globalData.jwt,
        "reply": this.data.reply,
        "user_id": wx.getStorageSync('user_id')
            },
      success: res => {
        console.log(this.data)
        if(res.data.status==0){
        wx.showToast({
            title: '提交成功'
          },
          wx.request({
            url : "http://" + app.globalData.url + "/cook/comment/" + app.globalData.comment_id + "/",
            method: 'GET',
            success: res => {
              console.log("that, comment", res)
              this.setData({
                comList: res.data.commentlist,
              }
              )
            }
          })
          
          )

        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })


  }})
